<?php


/*
 * Created on Jul 3, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php
require_once APPPATH."models/model_dao.php";
class Model_nav extends Model_dao {
	var $_public = 1;
	function Model_nav() {
		// Call the Model constructor
		parent::Model_dao("tbl_nav");
	}

	function fetch_all(){
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			return $result;
		}
	}
	/**
	 *
	 *
	 *
	 */
	function fetchNav($pid = 0, $sort = 'sort', $type_sort = 'asc', $limit = null, $offset = null, $show_footer=null) {
		$this->db->select('id, pid, level, nav_name, title, url_title, target, target_link');
		if (!is_null($pid)) {
			$this->db->where('pid', $pid);
		}
		if (!is_null($show_footer)) {
			$this->db->where('show_footer', $show_footer);
		}
		$this->db->where('publish', $this->_public);
		$this->db->order_by($sort, $type_sort);

		$query = $this->db->get($this->_tbl_name);
//		log_message('error', $this->db->last_query());
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			foreach($result as $key => $val){
				$result[$key]['children'] = $this->fetchNav($val['id']);
			}
			return $result;
		}
		return null;
	}

	function find($id) {
		if (intval($id)) {
			$query = $this->db->get_where($this->_tbl_name, array ('id' => $id));

			if ($query->num_rows() > 0) {
				return $query->first_row('array');
			}
		}
		return null;
	}

	/**
	 *
	 *
	 */
	function findByIds($ids) {
		$result = array ();
		//echo $this->db->last_query();
		foreach ($ids as $id) {
			$this->db->select("id, nav, head, nodelink");
			$this->db->where("{$this->_tbl_name}.id", $id);
			$query = $this->db->get($this->_tbl_name);
			if ($query->num_rows() > 0) {
				$result[] = $query->first_row('array');
			}
		}
		return $result;
	}

	/**
	 *
	 *
	 */
	function findnav($params, $sort = "sort", $type_sort = "ASC", $limit, $offset) {
		$this->db->select('*');
		if(!empty($params)){
			foreach($params as $key => $val){
				$this->db->where($key, $val);
			}
		}
		$this->db->order_by($sort, $type_sort);

		$query = $this->db->get($this->_tbl_name);
		$result = $query->result_array();
		//echo $this->db->last_query();
		return $result;
	}

	/**
	*
	*/
	function findTree($level, $id = 0, $pid = 0, $default = array()){
		//echo "---$level++$id++$pid--<br/>";
		if(intval($level)){
			$this->db->select('id, pid, nav_name, level');
			$this->db->where("level", $level);
			if(intval($level) and intval($pid)){
				$this->db->where("pid", $pid);
			}
			$this->db->order_by("sort");
			$query = $this->db->get($this->_tbl_name);
			$pid = 0;
			$tmp = array();
			if ($query->num_rows() > 0) {
	    		$data = $query->result_array();
				foreach($data as $key => $item){
					$tmp[$item['id']] = $item;
				}
				if(intval($id) && isset($tmp[$id])){
					$tmp[$id]['selected'] = true;
					$id = $tmp[$id]['pid'];
				}
				if(!intval($pid)){
					$item = $this->find($id);
					$pid = $item['pid'];
				}
			}
			else{
				// Get Parent Id
				$item = $this->find($id);
				$pid = $item['pid'];
			}
			$default[$level] = $tmp;
			if(intval($level) > 1){
				$default = $this->findTree($level-1, $id, $pid, $default);
			}
		}
		return $default;
	}

	/**
	 *
	 *
	 */
	function get_max_sort($level){
		$this->db->select_max('sort');
		$this->db->where('level', $level);
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$first_row = $query->first_row('array');
			return $first_row['sort'];
		}
		return 0;
	}

	/**
	 *
	 */
	function insert($data){
		$data['created_date'] = date("Y-m-d h:i:s");
		$data['modified_date'] = date("Y-m-d h:i:s");
		// Get max sort
		$max_sort = $this->get_max_sort($data['level']);
		$data['sort'] = $max_sort + 1;
		// URL Title
		if(isset($data['title'])){
			$data['url_title'] = post_slug($data['title']);
		}
		return parent::insert($data);
	}

	/**
	 *
	 */
	function update($id, $data) {
		if(intval($id)){
			$data['modified_date'] = date("Y-m-d h:i:s");
			// URL Title
			if(isset($data['title'])){
				$data['url_title'] = post_slug($data['title']);
			}
			$this->db->where('id', $id);
			$this->db->update($this->_tbl_name, $data);
		}
	}

	function delete($id){
		if(intval($id)){
			$this->db->delete($this->_tbl_name, array('id' => $id));
		}
	}
}
?>